<script setup>
import axios from 'axios'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const apiBaseUrl = import.meta.env.VITE_API_BASE_URL;  // 从环境变量获取基础 URL
const router = useRouter()

const username = ref('')
const userID = ref('')

const login = async () => {

  try {
    const response = await axios.post(`${apiBaseUrl}/guest`, {
      id: userID.value,
      name:username.value
    })
    // 如果登录成功，根据后端返回的响应进行页面跳转
    if ('room_id' in response.data) {
      alert(response.data.room_id);
      // 使用 router.push 方法将用户导航到成功页面
      localStorage.setItem('room', JSON.stringify({
        room_id: response.data.room_id,
      }));

      await router.push({name: 'GuestConsole'})
    } else {
      alert('登录失败，请检查用户名和密码')
    }
  } catch (error) {
    console.error('登录请求失败:', error)
    alert('登录请求失败')
  }
}


</script>
<template>
  <div class="base">
    <div class="login-container">
      <form @submit.prevent="login" style="top:0;">
        <div class="left-container">
          <div class="title">欢迎使用智能空调系统</div>
          <div class="input-container">
            <input v-model="userID" type="text" name="userID" placeholder="用户ID">
          </div>
        </div>
        <div class="right-container">
          <div class="action-container">
            <span><button type="submit">提交</button></span>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<style scoped>
.base{
  position: fixed;
  top: 0%;
  left: 0%;
  width: 100%; /* Adjust width as needed */
  height: 100%; /* Adjust height as needed */
  display: flex;
  justify-content: center;
  margin:auto 0;
  background-color: #372e4a;
  //background-image: linear-gradient(to bottom right, rgb(114, 135, 254), rgb(130, 88, 186));

}
.login-container {
  position: fixed;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 600px; /* Adjust width as needed */
  height: 315px; /* Adjust height as needed */
  display: flex;
  justify-content: center;
  margin: 10% auto 0;
  border-radius: 15px;
  box-shadow:
      0px 0.9px 5.2px -43px rgba(0, 0, 0, 0.058),
      0px 3.8px 11.9px -43px rgba(0, 0, 0, 0.1),
      0px 10.3px 21.4px -43px rgba(0, 0, 0, 0.137),
      0px 25.3px 37.2px -43px rgba(0, 0, 0, 0.18),
      0px 74px 69px -43px rgba(0, 0, 0, 0.28)
;
  background-image: linear-gradient(to bottom right, rgb(118, 76, 163), rgb(92, 103, 211));

}
.left-container {
  display: inline-block;
  width: 330px;
  border-top-left-radius: 15px;
  border-bottom-left-radius: 15px;
  padding: 60px;

}
.title {
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}
.title span {
  border-bottom: 3px solid rgb(237, 221, 22);
}
.input-container {
  padding: 20px 0;
}
input {
  border: 0;
  background: none;
  outline: none;
  color: #fff;
  margin: 20px 0;
  display: block;
  width: 100%;
  padding: 5px 0;
  transition: .2s;
  border-bottom: 1px solid rgb(199, 191, 219);
}
input:hover {
  border-bottom-color: #fff;
}
::-webkit-input-placeholder {
  color: rgb(199, 191, 219);
}
.message-container {
  font-size: 14px;
  transition: .2s;
  color: rgb(199, 191, 219);
  cursor: pointer;
}
.message-container:hover {
  color: #fff;
}
.right-container {
  width: 145px;
  display: inline-block;
  height: calc(100% - 120px);
  vertical-align: top;
  padding: 60px 0;
}
.regist-container {
  text-align: center;
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}
.regist-container span {
  border-bottom: 3px solid rgb(237, 221, 22);
}
.action-container {
  font-size: 10px;
  color: #fff;
  text-align: center;
  position: relative;
  top: 200px;
}
.action-container span {
  background-color: mediumpurple;
  border: 1px solid rgb(237, 221, 22);
  padding: 10px;
  display: inline;
  line-height: 20px;
  border-radius: 20px;
  position: absolute;
  bottom: 10px;
  left: calc(72px - 20px);
  transition: .2s;
  cursor: pointer;
}
.action-container span:hover {
  background-color: rgb(237, 221, 22);
  color: midnightblue;
}
button {
  background-color: transparent;
  border: none;  /* 移除边框 */
  color: #fff;  /* 设置文字颜色 */
  cursor: pointer;  /* 设置鼠标为指针形状 */
}

</style>